// page/component/new-pages/cart/cart.js
const request = require("../../utils/request.js");
const util = require("../../utils/util.js");
const app = getApp();
Page({
  data: {
    storeArr: [],
    selectAll: false,
    totalPrice: 0
  },
  onLoad: function() {

  },
  onShow: function() {
    var that = this;
    var shopTrolley = {}
    var url = request.URL + "/shopTrolley";
    request.request({
      url: url,
      data: {
        userId: 1
      },
      success: function(res) {
        shopTrolley = res.data;
        console.log(shopTrolley);
        var storeArr = [];
        for (var key in shopTrolley) {
          var storeInfo = new Object();
          storeInfo.storeId = key;
          var storeInfoArr = shopTrolley[key];
          if (storeInfoArr.length > 0) {
            storeInfo.goodsArr = storeInfoArr;
            var storeName = storeInfoArr[0].storeName;
            storeInfo.storeName = storeName;
            for (var i = 0; i < storeInfoArr.length; i++) {
              storeInfoArr[i].selected = false;
              var goodImgStr = storeInfoArr[i].goodImgArray;
              var goodImgArr = util.string2Array(goodImgStr);
              storeInfoArr[i].goodImgUrl = goodImgArr[0];
            }
          }
          storeInfo.selectedAllInStore = false;
          storeArr.push(storeInfo);
        }
        that.setData({
          storeArr: storeArr,
          selectAll: false,
          totalPrice: 0
        })
        console.log(that.data.storeArr);
      },
      fail: function() {
        request.showFailInfo()
      }
    })
  },
  /**选择物品 */
  selectItem: function(e) {
    var that = this;
    var selectedgoodId = e.target.dataset.goodid;
    var selectedStandard = e.target.dataset.standard;
    var totalPrice = that.data.totalPrice;
    var storeArr = that.data.storeArr;
    var selectAll = that.data.selectAll;
    for (var i = 0; i < storeArr.length; i++) {
      for (var j = 0; j < storeArr[i].goodsArr.length; j++) {
        var goodId = storeArr[i].goodsArr[j].goodId;
        var standard = storeArr[i].goodsArr[j].standard;
        if (selectedgoodId == goodId && storeArr[i].goodsArr[j].selected == false && selectedStandard == standard) {
          storeArr[i].goodsArr[j].selected = true;
          totalPrice += storeArr[i].goodsArr[j].goodPrice * storeArr[i].goodsArr[j].goodCnt;
          that.checkAll();
        } else if (selectedgoodId == goodId && storeArr[i].goodsArr[j].selected == true && selectedStandard == standard) {
          storeArr[i].goodsArr[j].selected = false;
          totalPrice -= storeArr[i].goodsArr[j].goodPrice * storeArr[i].goodsArr[j].goodCnt;
          that.checkAll();
        }
      }
    }
    that.setData({
      totalPrice: totalPrice,
    })
  },
  /**选择店铺所有物品 */
  selectItems: function(e) {
    var that = this;
    var selectedStoreId = e.target.dataset.storeid;
    var storeArr = that.data.storeArr;
    var totalPrice = that.data.totalPrice;
    var selectAll = that.data.selectAll;
    for (var i = 0; i < storeArr.length; i++) {
      var storeId = storeArr[i].storeId;
      if (selectedStoreId == storeId && storeArr[i].selectedAllInStore == true) {
        storeArr[i].selectedAllInStore = false;
        for (var j = 0; j < storeArr[i].goodsArr.length; j++) {
          if (storeArr[i].goodsArr[j].selected == true) {
            storeArr[i].goodsArr[j].selected = false;
            totalPrice -= storeArr[i].goodsArr[j].goodPrice * storeArr[i].goodsArr[j].goodCnt;
          }
        }
        that.checkAll();
      } else if (selectedStoreId == storeId && storeArr[i].selectedAllInStore == false) {
        storeArr[i].selectedAllInStore = true;
        for (var j = 0; j < storeArr[i].goodsArr.length; j++) {
          if (storeArr[i].goodsArr[j].selected == false) {
            storeArr[i].goodsArr[j].selected = true;
            totalPrice += storeArr[i].goodsArr[j].goodPrice * storeArr[i].goodsArr[j].goodCnt;
          }
        }
        that.checkAll();
      }
    }
    that.setData({
      totalPrice: totalPrice,
    })
  },
  /**选择所有商品 */
  selectOrUnselectAll: function(e) {
    var that = this;
    var storeArr = that.data.storeArr;
    var selectAll = that.data.selectAll;
    var totalPrice = that.data.totalPrice;
    if (selectAll == false) {
      for (var i = 0; i < storeArr.length; i++) {
        storeArr[i].selectedAllInStore = true;
        for (var j = 0; j < storeArr[i].goodsArr.length; j++) {
          if (!storeArr[i].goodsArr[j].selected) {
            storeArr[i].goodsArr[j].selected = true;
            totalPrice += storeArr[i].goodsArr[j].goodPrice * storeArr[i].goodsArr[j].goodCnt;
          }
        }
      }
      selectAll = true;
    } else {
      for (var i = 0; i < storeArr.length; i++) {
        storeArr[i].selectedAllInStore = false;
        for (var j = 0; j < storeArr[i].goodsArr.length; j++) {
          if (storeArr[i].goodsArr[j].selected) {
            storeArr[i].goodsArr[j].selected = false;
            totalPrice -= storeArr[i].goodsArr[j].goodPrice * storeArr[i].goodsArr[j].goodCnt;
          }
        }
      }
      selectAll = false;
    }
    that.setData({
      storeArr: storeArr,
      selectAll: selectAll,
      totalPrice: totalPrice
    })
  },
  /**判断选择全店铺产品和全部产品 */
  checkAll: function(e) {
    var that = this;
    var selectAll = that.data.selectAll;
    var storeArr = that.data.storeArr;
    for (var i = 0; i < storeArr.length; i++) {
      for (var j = 0; j < storeArr[i].goodsArr.length; j++) {
        if (!storeArr[i].goodsArr[j].selected) {
          storeArr[i].selectedAllInStore = false;
          break;
        }
        storeArr[i].selectedAllInStore = true;
      }
    }

    for (var i = 0; i < storeArr.length; i++) {
      if (!storeArr[i].selectedAllInStore) {
        selectAll = false;
        break;
      }
      selectAll = true;
    }
    that.setData({
      storeArr: storeArr,
      selectAll: selectAll
    })
  },
  /**商品数加一 */
  addOne: function(e) {
    var that = this;
    var url = request.URL + '/shopTrolley/updateCnt';
    var shopTrolleyId = e.currentTarget.dataset.shoptrolleyid;
    request.request({
      url: url,
      data: {
        shopTrolleyId: shopTrolleyId,
        flag: 1,
        count:-1
      },
      success: function(res) {
        console.log(res);
        that.onShow();
      },
      fail: function() {
        request.showFailInfo()
      }
    })
  },
  /**商品数减一 */
  minusOne: function(e) {
    var that = this;
    var url = request.URL + '/shopTrolley/updateCnt';
    var shopTrolleyId = e.currentTarget.dataset.shoptrolleyid;
    request.request({
      url: url,
      data: {
        shopTrolleyId: shopTrolleyId,
        flag: -1,
        count: -1
      },
      success: function(res) {
        console.log(res);
        that.onShow();
      },
      fail: function() {
        request.showFailInfo()
      }
    })
  },
  /**点击删除图标删除 */
  deleteItem: function(e) {
    var that = this;
    var url = request.URL + '/shopTrolley/delete';
    var shopTrolleyId = e.currentTarget.dataset.shoptrolleyid;
    var shopTrolleyIdArray = [];
    shopTrolleyIdArray.push(shopTrolleyId);
    request.request({
      url: url,
      data: {
        shopTrolleyIdArray: shopTrolleyIdArray,
      },
      success: function(res) {
        console.log(res);
        that.onShow();
      },
      fail: function() {
        request.showFailInfo()
      }
    })
  },
  /**删除所有选择的商品 */
  deleteAll: function(e) {
    var that = this;
    var url = request.URL + '/shopTrolley/delete';
    var shopTrolleyIdArray = [];
    var storeArr = that.data.storeArr;
    console.log(storeArr);
    for (var i = 0; i < storeArr.length; i++) {
      for (var j = 0; j < storeArr[i].goodsArr.length; j++) {
        var shopTrolleyId = storeArr[i].goodsArr[j].shopTrolleyId;
        if (storeArr[i].goodsArr[j].selected) {
          shopTrolleyIdArray.push(shopTrolleyId);
        }
      }
    }
    request.request({
      url: url,
      data: {
        shopTrolleyIdArray: shopTrolleyIdArray,
      },
      success: function(res) {
        console.log(res);
        that.onShow();
      },
      fail: function() {
        request.showFailInfo()
      }
    })
  },
  /**进入确认订单页 */
  toOrderComfirm: function(e) {
    var that = this;
    var storeArr = that.data.storeArr;
    var selectedItemNum = 0;
    var orderItemsArr = [];
    var fromItemPage = false;
    for (var i = 0; i < storeArr.length; i++) {
      if (storeArr[i].selectedAllInStore) {
        orderItemsArr.push(storeArr[i]);
        selectedItemNum += storeArr[i].goodsArr.length;
      } else {
        var goodsArr = storeArr[i].goodsArr;
        console.log(goodsArr)
        var tempArr = [];
        for (var j = 0; j < goodsArr.length; j++) {
          if (goodsArr[j].selected) {
            selectedItemNum += 1;
            tempArr.push(goodsArr[j]);
          } else {
            continue;
          }
        }
        storeArr[i].goodsArr = tempArr;
        orderItemsArr.push(storeArr[i]);
      }
    }
    orderItemsArr.fromItemPage = fromItemPage;
    app.globalData.orderItemsArr = orderItemsArr;
    console.log(app.globalData.orderItemsArr);
    wx.navigateTo({
      url: '/pages/order_list/order_confirm/order_confirm',
    })
  },
  /**进入店铺 */
  toStore: function (e) {
    var storeId = e.currentTarget.dataset.storeid;
    wx.navigateTo({
      url: '/pages/details/shop_details/shop_details?storeId=' + storeId,
    })
  },
  changeItemNum: function (e) {
    var value = parseInt(e.detail.value);
    var that = this;
    var url = request.URL + '/shopTrolley/updateCnt';
    var shopTrolleyId = e.currentTarget.dataset.shoptrolleyid;
    request.request({
      url: url,
      data: {
        shopTrolleyId: shopTrolleyId,
        flag: 0,
        count: value
      },
      success: function (res) {
        console.log(res);
        that.onShow();
      },
      fail: function () {
        request.showFailInfo()
      }
    })
  },
  //转发小程序
  onShareAppMessage: function () {
    return {
      title: '呆狗商城',
      path: '/pages/index/index',
      success: function (res) {
        //转发成功
        wx.showToast({
          title: '转发成功',
          icon: 'success'
        })
      },
      fail: function (res) {
        //转发失败
      }
    }
  },

  toHomePage: function(e) {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  /**点击进入商品详情页 */
  toGoodDetails: function (e) {
    var goodId = e.currentTarget.dataset.goodid;
    wx.navigateTo({
      url: '/pages/details/item_details/item_details?goodId=' + goodId,
    })
  }
})